# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import scrapy
from scrapy.pipelines.images import ImagesPipeline


class MiddePipeline:
    def process_item(self, item, spider):
        return item


class ImgsPipeline(ImagesPipeline):
    # 发送请求的方法，根据图片发起请求
    def get_media_requests(self, item, info):
        yield scrapy.Request(item["src"], meta={"item": item})

    # 返回图片名称
    def file_path(self, request, response=None, info=None, *, item=None):
        item = request.meta["item"]
        # 通过 request 得到，不是通过 response 得到
        filename = item["name"]
        return filename

    # 将 item 传输给下一个即可
    def item_completed(self, results, item, info):
        print(f"""{item["name"]}爬取成功，请前往查看""")
        return item
